/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mobiwebinc.compconn.configuration;

import com.mobiwebinc.compconn.communication.SystemController;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

/**
 *
 * @author suraj
 */
public class ServerConfiguration extends Configuration {

    private static ServerConfiguration serverConfiguration;
    private String password;
    private String oSName;
    private boolean visible = true;
    private int port;
    private static boolean loaded = false;
    private int monitors;
    private Preferences pref = Preferences.userNodeForPackage(this.getClass());
    private Rectangle[] screenRects;

    private ServerConfiguration() {
        reloadScreens();
    }

    public static ServerConfiguration getInstance() {
        if (serverConfiguration == null) {
            serverConfiguration = new ServerConfiguration();
            if (!loaded) {
                serverConfiguration.loadServerConfiguration();
            }
        }
        return serverConfiguration;
    }

    private void loadServerConfiguration() {
        try {
            pref.sync();
        } catch (BackingStoreException ex) {
            Logger.getLogger(ServerConfiguration.class.getName()).log(Level.SEVERE, null, ex);
        }
        setName(pref.get(Constant.NAME, MachineInfo.getMachineName()));
        setPort(pref.getInt(Constant.PORT, 25684));
        setVisible(pref.getBoolean(Constant.VISIBLE, false));
        setPassword(pref.get(Constant.PASSWORD, ""));
        setOSName(pref.get(Constant.OS_NAME, System.getProperty("os.name")));
        setMonitors();
        loaded = true;
    }

    public void saveServerConfiguration() {
        pref.put(Constant.NAME, getName());
        pref.put(Constant.PASSWORD, getPassword());
        pref.put(Constant.OS_NAME, getOSName());
        pref.putInt(Constant.PORT, getPort());
        pref.putBoolean(Constant.VISIBLE, isVisible());
        try {
            pref.flush();
        } catch (BackingStoreException ex) {
            Logger.getLogger(ServerConfiguration.class.getName()).log(Level.SEVERE, null, ex);
        }
        loadServerConfiguration();
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the port
     */
    public int getPort() {
        return port;
    }

    /**
     * @param port the port to set
     */
    public void setPort(int port) {
        this.port = port;
    }

    /**
     * @return the oSName
     */
    public String getOSName() {
        return oSName;
    }

    /**
     * @param oSName the oSName to set
     */
    public void setOSName(String oSName) {
        this.oSName = oSName;
    }

    /**
     * @return the visible
     */
    public boolean isVisible() {
        return visible;
    }

    /**
     * @param visible the visible to set
     */
    public void setVisible(boolean visible) {
        this.visible = visible;
    }

    public String getOSCode() {
        return oSName.substring(0, 1).toUpperCase();
    }

    /**
     * @return the monitors
     */
    public int getMonitors() {
        return monitors;
    }

    /**
     * @param monitors the monitors to set
     */
    public void setMonitors() {
        this.monitors = SystemController.getInstance().getMonitorCount();
    }

    public int commandKey() {
        return getOSCode().equals("M") ? KeyEvent.VK_META : KeyEvent.VK_CONTROL;
    }

    public int appSwitchKey() {
        return getOSCode().equals("M") ? KeyEvent.VK_META : KeyEvent.VK_ALT;
    }

    public int windowSwitcher() {
        return getOSCode().equals("M") ? KeyEvent.VK_BACK_QUOTE : KeyEvent.VK_TAB;
    }

    /**
     * @return the screenRects
     */
    public Rectangle[] getScreenRects() {
        return screenRects;
    }

    /**
     * @param screenRects the screenRects to set
     */
    public void setScreenRects(Rectangle[] screenRects) {
        this.screenRects = screenRects;
    }

    public Rectangle getScreenRect(int monitor) {
        return screenRects[monitor];
    }

    public Rectangle[] getCleanedScreenRects() {
        Rectangle[] rects = new Rectangle[screenRects.length];
        for(int i=0;i<screenRects.length;i++) {
            rects[i] = new Rectangle(screenRects[i]);
            rects[i].x = rects[i].y = 0;
        }
        return rects;
    }

    public void reloadScreens() {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] gds = ge.getScreenDevices();
        screenRects = new Rectangle[gds.length];
        for (int i = 0; i < gds.length; i++) {
            DisplayMode mode = gds[i].getDisplayMode();
            screenRects[i] = gds[i].getDefaultConfiguration().getBounds();
        }
        setScreenRect(screenRects[0]);
    }
}
